import pandas as pd
from local.processor import *
from nlp import *
class Group:
    bins =  [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    labels =  [f'{i}-{i + 10}' for i in range(0, 100, 10)]
    age_col = '年龄int'
def remove_invalid_age(df,age):
    df = df[df['年龄int'] > age]
    return df

def num_group_df(df,age_col,bins,labels):
    age_groups = pd.cut(df[age_col], bins=bins, labels=labels,
                        include_lowest=True)
    print('好大夫')
    # 计算每个区间的数据数量
    age_counts = age_groups.value_counts().sort_index()
    print(age_counts)
    print('平均值', int(df['年龄int'].mean()), '最大', df['年龄int'].max(), '样本量', len(df))

def process_dingxiang():
    """
    丁香
    :return:
    """
    dingxiang = read_cache('wenzhen/dingxiang.pk')
    #patient_health_record.patient_age
    print(len(dingxiang))
    res = []
    for e in dingxiang:
        res.append((e['search_key'],e['patient_health_record']['patient_age'],e['patient_health_record']['patient_nick_name'],e.get('content','')+'|'+e.get('answer','')))

    df = pd.DataFrame(res,columns=['搜索词','年龄','名字','问诊文本'])
    df['任意搜索词'] = df['问诊文本'].str.contains('|'.join(df['搜索词'].unique()))
    df = df[df['年龄'].str.contains('岁')]
    df = df[~df['年龄'].str.contains('月|天')]
    df = df[~df['问诊文本'].str.contains('小孩|儿子')]

    df = df[df['任意搜索词']]
    df['年龄int']= df['年龄'].apply(lambda x:int(x.replace('岁','')))
    #df =df[df['年龄int']>30]
    print(df['年龄int'].describe())
    # 定义年龄区间
    num_group_df(df, Group.age_col, Group.bins, Group.labels)
    df.to_excel('wenzhen/病人信息_丁香.xlsx')

def process_haodaifu():
    """
    好大夫
    :return:
    """
    path = 'wenzhen/haodaifu.pk'
    coll_data = read_cache(path)
    res = []
    for e in coll_data:
        row = [e['search_key'],e.get('patient_info',''),e.get('item_title','')+e.get('item_content','')]
        res.append(row)
    df = pd.DataFrame(res, columns=['搜索词','病人信息', '问诊文本'])
    df['任意搜索词'] = df['问诊文本'].str.contains('|'.join(df['搜索词'].unique()))
    df = df[df['任意搜索词']]
    df = df[df['病人信息'].str.contains('岁')]
    df['年龄int'] = df['病人信息'].apply(lambda x: get_age(x))
    #df = df[df['年龄int']>30]
    age_col = '年龄int'
    num_group_df(df, Group.age_col, Group.bins, Group.labels)
    df.to_excel('wenzhen/病人信息_好大夫.xlsx')

def process_chunyu():
    """
    春雨
    :return:
    """
    path = 'wenzhen/chunyu.pk'
    coll_data = read_cache(path)
    #item_title	item_content
    res = []
    for e in coll_data:
        row = [e['search_key'],e.get('item_title',''),e.get('item_content',''),e.get('item_title','')+e.get('item_content','')]
        res.append(row)
    df = pd.DataFrame(res, columns=['搜索词',   '问题',"回复", '问诊文本'])
    print('原本数据量',len(df))
    df['任意搜索词'] = df['问诊文本'].str.contains('|'.join(df['搜索词'].unique()))
    df = df[df['任意搜索词']]
    print('原本数据量2', len(df))
    df = df[df['问题'].str.contains('岁')]
    df['年龄int']= df['问题'].apply(lambda x:get_age(x))
    #df = df[df['年龄int']>30]
    num_group_df(df, Group.age_col, Group.bins, Group.labels)
    df.to_excel('wenzhen/病人信息_春雨.xlsx')

def get_all_wenzhen():
    """
    dingxiang
chunyu
weiyi
haodaifu
    :return:
    """
    rows = []
    for c in ['dingxiang','chunyu','haodaifu']:
        data_C = read_cache(f'wenzhen/{c}.pk')


        for e in data_C:
            if c == 'dingxiang':
                row  = [c,'','',e['content'],e['answer'],'']
            elif c =='chunyu':
                #item_title	item_content	问诊总结.问题描述	问诊总结.分析及建议	交流记录
                print(e.keys())
                if '问诊总结' not in e:
                    continue
                row = [c,e['item_title'],e['item_content'],e['问诊总结']['问题描述'],e['问诊总结']['分析及建议'],e['交流记录'] ]
            elif c =='haodaifu':
                #item_title	item_content	病例信息.*	问诊建议.*	交流记录
                q = ''
                if '病例信息' in e:
                    for k,v in e['病例信息'].items():
                        q+=f'{k}:{v}\n'
                a = ''
                if '问诊建议' in e:
                    for k, v in e['问诊建议'].items():
                        a += f'{k}:{v}\n'
                row = [c,e['item_title'],e['item_content'],q,a,e.get('交流记录','')]
            rows.append(row)
    df = pd.DataFrame(rows,columns=['平台','标题','内容','病人','医生','对话'])
    #df.to_excel('全部问诊.xlsx')
    df.to_csv('全部问诊.csv')

def process_wenzhen_type():
    """
    查找所有的类型
    :return:
    """
    dingxiang = read_cache('wenzhen/dingxiang.pk')
    #patient_health_record.patient_age
    print(len(dingxiang))
    res = []
    import collections
    c = collections.Counter([e['item_type'] for e in dingxiang])
    print(c)

    """
    plat2query = {
    "chunyu": {"platform": "chunyu", "url_type": "disease"},
    "haodaifu": {"platform": "haodaifu", "url_type": "wenzhen"},
    "dingxiang": {"platform": "dingxiang", "item_type": "wenzhen"},
    "weiyi": {"platform": "weiyi" },
}
    """
    print('春雨')
    chunyu = read_cache('wenzhen/chunyu.pk')
    c = collections.Counter([e['url_type'] for e in chunyu])
    print(c)

    print('好大夫')
    chunyu = read_cache('wenzhen/haodaifu.pk')
    c = collections.Counter([e['url_type'] for e in chunyu])
    print(c)

#get_all_wenzhen()
#process_dingxiang_type()